package com.sinven.daoImpl;

import com.sinven.dao.CommentDao;
import com.sinven.entity.Comment;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;

@Repository
public class CommentDaoImpl implements CommentDao {
    @Autowired
    @Qualifier("hibernateTemplate")
    private HibernateTemplate hibernateTemplate;

    @Autowired
    @Qualifier("sessionFactory")
    private SessionFactory sessionFactory;

    @Override
    public List<Comment> listComment(String hql) {
        Session currentSession = sessionFactory.getCurrentSession();
        Query query = currentSession.createQuery(hql);
        return query.list();
    }

    @Override
    public int addComment(Comment comment) {
        Session currentSession = sessionFactory.getCurrentSession();
        Serializable save = currentSession.save(comment);
        return (Integer)save;
    }

    @Override
    public List<Comment> selectCommentBygoodsId(Integer goodsId) {
        String hql="from Comment where goodsId=:goodsId";
        Session currentSession = sessionFactory.getCurrentSession();
        Query query = currentSession.createQuery(hql);
        query.setParameter("goodsId",goodsId);
        return query.list();
    }

    @Override
    public List<Comment> selectCommentByCommentId(Integer commId) {
        String hql="from Comment where id=:commId";
        Session currentSession = sessionFactory.getCurrentSession();
        Query query = currentSession.createQuery(hql);
        query.setParameter("commId",commId);
        return query.list();
    }
}
